package com.dzl.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.manager.ManagerService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.HttpCode;
import com.dzl.util.JsonResult;
import com.dzl.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@Description:通用相关接口
*@author pengkailun
*@time:2021年4月24日下午4:14:40
 */
@RestController
@RequestMapping("/manager")
public class ManagerController extends BaseController{


	@Autowired
	private ManagerService managerService;

	/**
	 * 手机短信发送
	 * @param obj(phone 手机号 ,type 1注册发送 2忘记密码发送 3余额提现)
	 * @author pengkailun
	 * @time 2021年2月23日15:58:21
	 * @return
	 */
	@PostMapping("/sendCode")
	public JsonResult<String> sendCode(@RequestBody JSONObject obj){
		String phone = obj.getString("phone");
		String type = obj.getString("type");
		Integer enterpriseId = obj.getInteger("enterpriseId");
		if(EmptyUtils.isEmpty(phone)) {
			return new JsonResult<String>(null,HttpCode.PHONE_ERROR);
		}
		String code = managerService.sendCode(enterpriseId==null?0:enterpriseId,phone, type);
		return JsonResult.success();
	}

	/**
	 * 图片上传
	 * @param request
	 * @return com.libei.util.JsonResult
	 * @date 2020/11/26 18:41
	 * @author lishunxing
	 */
	@RequestMapping("/uploadImg")
	public JsonResult uploadImg(HttpServletRequest request) {
		String result = UploadUtil.uploadImg(request);
		if(EmptyUtils.isNotEmpty(result) && result.contains("https://")){
			return JsonResult.success(result);
		}else{
			return JsonResult.failWithMsg("上传图片失败:"+result);
		}
	}

	/**
	 * wangEditor富文本编辑器图片上传
	 * @date 2021/4/29 17:19
	 * @author lishunxing
	 * @param request 图片请求
	 * @return java.util.Map wangEditor解析对象
	 */
	@RequestMapping("/uploadImg/wangEditor")
	public Map uploadWangEditorImg(HttpServletRequest request) {
		String imgUrl = UploadUtil.uploadImg(request);
		if(!(EmptyUtils.isNotEmpty(imgUrl) && imgUrl.contains("https://"))){
			Map result = new HashMap();
			result.put("errno", -1);
			result.put("data", imgUrl);
			return result;
		}
		System.out.println("图片上传完毕 : " + imgUrl);
		String[] split = imgUrl.split(",");
		List<Map> imgList = new ArrayList<>(split.length);
		for (String s : split) {
			Map<String,String> map = new HashMap(3);
			map.put("alt", "图片");
			map.put("url", s);
			map.put("href", s);
			imgList.add(map);
		}
		Map result = new HashMap();
		result.put("errno", 0);
		result.put("data", imgList);
		return result;
	}

	/**
	 * 视频上传
	 * @date 2021/5/20 16:18
	 * @author caobucao
	 * @param request
	 * @return com.dzl.util.JsonResult
	 */
	@RequestMapping("/uploadVideo")
	public JsonResult uploadVideo(HttpServletRequest request) {
		//String videoUrl = UploadUtil.uploadVideo(request);
		String result = UploadUtil.uploadVideo(request);
		if(EmptyUtils.isNotEmpty(result) && result.contains("https://")){
			return JsonResult.success(result);
		}else{
			return JsonResult.failWithMsg("上传视频失败:"+result);
		}
	}
}
